package com.example.demo.java8.stream;

import com.example.demo.model.Student;
import com.google.common.collect.Lists;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 *java 8 stream
 *
 * List转map
 * @author luowj12
 * @since 2021/5/27 16:31
 */
public class ListToMapTest {

    public static void main(String[] args) {

        List<Map> list = new ArrayList();
        Map map = new HashMap();
        map.put("WZPM","aa");
        map.put("sl",11);
        list.add(map);
        Map map1 = new HashMap();
        map1.put("WZPM","aa");
        map1.put("sl",1122);
        list.add(map1);
        Map mapdata = new HashMap();
        mapdata.put("WZPM","bb");
        mapdata.put("sl",666);
        list.add(mapdata);
        //(key1,key2)->key1) 代表 key重复了，取前面的
        Map map2 = list.stream().collect(Collectors.toMap(x -> x.get("WZPM").toString(),
                x -> x.get("sl").toString(),(key1,key2)->key1));

//        Map map3 = list.stream().collect(Collectors.toMap(x -> x.get("WZPM").toString(),
//                x ->(value1,value2)->{
//                    return value1;
//                }));

        List<Student> sList = new ArrayList<>();
        Student s1 = new Student("aa",22);
        Student s2 = new Student("bb",33);
        Student s3 = new Student("cc",22);
        sList.add(s1);
        sList.add(s2);
        sList.add(s3);
        //key相同 对value进行处理
        Map map3 = sList.stream().collect(Collectors.toMap(Student::getSex,
                user->user.getName(),(name1,name2)->name1+","+name2));
        
        System.out.println(map3);

        List<Student> sList1 = new ArrayList<>();
        Student s11 = new Student("aa",22);
        Student s12 = new Student("aa",33);
        Student s13 = new Student("xx",22);
        sList1.add(s11);
        sList1.add(s12);
        sList1.add(s13);
        //name 相同，把sex放在一个list中
        Map<String,List<Integer>> map4 = sList1.stream().collect(Collectors.toMap(Student::getName,
                user-> Lists.newArrayList(user.getSex()),(sex1, sex2)->{
                    sex1.addAll(sex2);
                    return sex1;
                }));
        //name 相同，把sex放在一个list中
        Map map41 = sList1.stream().collect(Collectors.toMap(Student::getName,
                (user)->{
                    List l =  new ArrayList();
                    l.add(user);
                    return l;
                },(sex1, sex2)->{
                    sex1.addAll(sex2);
                    return sex1;
                }));

        Map map42 = sList1.stream().collect( Collectors.groupingBy(Student::getName, Collectors.mapping(Student::getSex, Collectors.toList())));
        //name 相同，相加
        Map map5 = sList1.stream().collect(Collectors.toMap(Student::getName,
                user-> user.getSex(),(sex1, sex2)->{
                    return sex1+sex2;
                }));

        System.out.println(map4);

        List<Student> sList2 = new ArrayList<>();
        Student s21 = new Student("aa",100,1);
        Student s22 = new Student("bb",200,2);
        Student s23 = new Student("xx",300,1);
        Student s24 = new Student("cc",300,1);
        sList2.add(s21);
        sList2.add(s22);
        sList2.add(s23);
        sList2.add(s24);
        Map map6  = sList2.stream().collect(Collectors.groupingBy(Student::getSex, Collectors.groupingBy(Student::getSalary)));
        /*Map map6 = sList2.stream().collect(Collectors.toMap(Student::getSex+Student::getSalary,
                user->user.getName(),(key1,key2)->key1));
        System.out.println(map6);*/
    }

}
